package cn.edu.fzu.delivery.controller.customer;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.domain.entity.Address;
import cn.edu.fzu.delivery.domain.entity.Customer;
import cn.edu.fzu.delivery.service.customer.AddressService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 顾客地址管理
 * 
 * @author fangxin
 * @date 2022/11/3
 */
@EnableOpenApi
@RestController
@RequestMapping("/api/auth/customer/address")
@Api(tags = "顾客-地址管理")
public class AddressController {

    @Resource
    private AddressService addressService;

    /** 查询所有地址 **/
    @GetMapping("/list")
    @ApiOperation(value = "查询顾客所有地址")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", required = true,value = "pageNum 从 1 开始",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", required = true, defaultValue = "10") })
    public Result list(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<Address> page = PageHelper.startPage(pageNum, pageSize)) {
            addressService.list(customer.getId());
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    /**
     * 添加地址
     *
     * @param name 姓名
     * @param phone 手机号
     * @param networkId 网点id
     * @param address 详细地址
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "顾客添加地址", httpMethod = "POST", notes = "需请获取地区id")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", required = true, value = "姓名"),
            @ApiImplicitParam(name = "phone", required = true, value = "手机号"),
            @ApiImplicitParam(name = "email", required = true, value = "邮箱"),
            @ApiImplicitParam(name = "networkId", required = true, value = "网点id"),
            @ApiImplicitParam(name = "address", required = true, value = "详细地址") })
    public Result add(String name, String phone, String email, Long networkId, String address,
                      HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        addressService.add(customer.getId(), name, phone, email, networkId, address);
        return Result.SUCCESS;
    }


    @PostMapping("/update")
    @ApiOperation(value = "顾客修改地址", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "地址id"),
            @ApiImplicitParam(name = "name", required = false, value = "姓名"),
            @ApiImplicitParam(name = "phone", required = false, value = "手机号"),
            @ApiImplicitParam(name = "email", required = false, value = "邮箱"),
            @ApiImplicitParam(name = "networkId", required = false, value = "网点id"),
            @ApiImplicitParam(name = "address", required = false, value = "详细地址") })
    public Result update(Long id, String name, String phone, String email, Long networkId, String address,
                         HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        addressService.update(customer.getId(), id, name, phone, email, networkId, address);
        return Result.SUCCESS;
    }


    @GetMapping("/delete")
    @ApiOperation(value = "顾客删除地址", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "地址id") })
    public Result delete(Long id, HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        addressService.delete(customer.getId(), id);
        return Result.SUCCESS;
    }
}
